package org.jeecg.modules.sssj.controller;

import java.text.SimpleDateFormat;
import java.util.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.config.annotations.OrgPermissionAnnotation;
import org.jeecg.config.util.CurrentOrgInfoUtil;
import org.jeecg.modules.qyxx.entity.BgSjmkjgOrder;
import org.jeecg.modules.qyxx.entity.vo.BgFpHzYfQtzyzcSssjQueryVo;
import org.jeecg.modules.qyxx.entity.vo.BgFpHzYfQtzyzcSssjVo;
import org.jeecg.modules.qyxx.service.IBgFpHzNdQtzyzcService;
import org.jeecg.modules.qyxx.service.IBgFpHzYfQtzyzcService;
import org.jeecg.modules.qyxx.service.IBgSjmkjgOrderService;
import org.jeecg.modules.qyxx.util.DateUtils;
import org.jeecg.modules.sssj.dto.FphzDto;
import org.jeecg.modules.sssj.dto.FphzQtzcTbDto;
import org.jeecg.modules.sssj.dto.MaxSsqDto;
import org.jeecg.modules.sssj.entity.SssjFphz;
import org.jeecg.modules.sssj.service.ISssjFphzService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.sssj.vo.FphzQtzcVo;
import org.jeecg.modules.sssj.vo.FphzVo;
import org.jeecg.modules.sssj.vo.FpzlhzVo;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 实时数据-发票汇总
 * @Author: jeecg-boot
 * @Date:   2022-06-13
 * @Version: V1.0
 */
@Slf4j
@Api(tags="实时数据-发票汇总")
@RestController
@RequestMapping("/sssj/sssjFphz")
public class SssjFphzController extends JeecgController<SssjFphz, ISssjFphzService> {
	@Autowired
	private ISssjFphzService sssjFphzService;
	@Autowired
	private IBgFpHzYfQtzyzcService bgFpHzYfQtzyzcService;
	@Autowired
	private IBgFpHzNdQtzyzcService bgFpHzNdQtzyzcService;
	@Autowired
	private IBgSjmkjgOrderService bgSjmkjgOrderService;

	 @ApiOperation(value="发票汇总")
	 @OrgPermissionAnnotation
	 @PostMapping(value = "/fphz")
	 public Result<FphzVo> fphz(@Validated @RequestBody FphzDto fphzDto) {
		 String tynsrsbh = CurrentOrgInfoUtil.getCurrentOrgShxydm();
		 FphzVo fphzVo = sssjFphzService.selectNsrFphz(tynsrsbh, fphzDto.getSsny(), fphzDto.getJxxlx());
		 return Result.OK(fphzVo);
	 }

	 @ApiOperation(value="发票种类汇总")
	 @OrgPermissionAnnotation
	 @PostMapping(value = "/fpzlhz")
	 public Result<List<FpzlhzVo>> fpzlhz(@Validated @RequestBody FphzDto fphzDto) {
		 String tynsrsbh = CurrentOrgInfoUtil.getCurrentOrgShxydm();
		 List<FpzlhzVo> fpzlhzList = sssjFphzService.selectNsrFpzlHz(tynsrsbh, fphzDto.getSsny(), fphzDto.getJxxlx());
		 return Result.OK(fpzlhzList);
	 }

	 @ApiOperation(value = "查询纳税人发票汇总最大年月")
	 @OrgPermissionAnnotation
	 @PostMapping(value = "/getNsrMaxSsny")
	 public Result<String> getNsrMaxSsny(@Validated @RequestBody MaxSsqDto maxSsqDto) {
		 String tynsrsbh = CurrentOrgInfoUtil.getCurrentOrgShxydm();
		 String maxSsqq = sssjFphzService.getNsrMaxSsny(tynsrsbh);
		 return Result.OK(maxSsqq);
	 }

	 @ApiOperation(value = "查询发票汇总其他支出同比")
	 @OrgPermissionAnnotation
	 @PostMapping(value = "/listFpHzYfQtzyzcTb")
	 public Result<?> listFpHzYfQtzyzcTb(@Validated @RequestBody FphzQtzcTbDto fphzQtzcTbDto) {
		 String tynsrsbh = CurrentOrgInfoUtil.getCurrentOrgShxydm();
	 	BgSjmkjgOrder order = bgSjmkjgOrderService.getLatestByDjxh(tynsrsbh);
		 if (order == null) {
			 return Result.error("查询不到企业发票汇总其他支出数据");
		 }
		 Calendar calendar = Calendar.getInstance();
		 calendar.setTime(order.getJssj());
		 Integer year = calendar.get(Calendar.YEAR);
		 List<String> yearList = Arrays.asList((year - 2) + "", (year - 1) + "", year + "");
		 Map fpHzNdQtzyzcData = bgFpHzNdQtzyzcService.fpHzNdQtzyzc(tynsrsbh, yearList, order.getJssj());

		 BgFpHzYfQtzyzcSssjQueryVo vo = new BgFpHzYfQtzyzcSssjQueryVo();
		 vo.setDjxh(tynsrsbh);
		 vo.setYear(year);
		 List<BgFpHzYfQtzyzcSssjVo> bgFpHzYfQtzyzcSssjVos = bgFpHzYfQtzyzcService.listSssjByDjxhAndYears(vo);

		 FphzQtzcVo respData = new FphzQtzcVo();
		 respData.setData(bgFpHzYfQtzyzcSssjVos);
		 respData.setYearList(yearList);
		 respData.setFpHzNdQtzyzcData(fpHzNdQtzyzcData);
		 return Result.OK(respData);
	 }
}
